package com.example.service;

import com.example.common.Result;
import com.example.dao.BookDao;
import com.example.dao.StudentDao;
import com.example.dao.TypeDao;
import com.example.entity.Book;
import com.example.entity.Params;
import com.example.entity.Type;
import com.example.entity.dto.InfoDTO;
import com.example.entity.dto.UserDTO;
import com.example.entity.dto.studentInforDTO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class BookService {

    @Resource
    private BookDao bookDao;
    @Resource
    private TypeDao typeDao;

    @Resource
    private StudentDao studentDao;


    public PageInfo<Book> findBySearch(Params params) {
        // 开启分页查询
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        // 接下来的查询会自动按照当前开启的分页设置来查询
        List<Book> list = bookDao.findBySearch(params);
        return PageInfo.of(list);
    }
    public PageInfo<Book> findBysearchExport(Params params) {
        // 开启分页查询
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        // 接下来的查询会自动按照当前开启的分页设置来查询
        List<Book> list = bookDao.findBysearchExport(params);
        return PageInfo.of(list);
    }


    public PageInfo<Book> findBySearchNotExport(Params params) {
        // 开启分页查询
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        // 接下来的查询会自动按照当前开启的分页设置来查询
        List<Book> list = bookDao.findBySearchNotExport(params);
        return PageInfo.of(list);
    }


    public void addStudent(UserDTO userDTO) {
        InfoDTO infoDTO =  typeDao.selectInforByIdCard(userDTO);
        studentInforDTO studentInforDTO=studentDao.selectstudentInforByIdCard(userDTO);





        Book book = new Book();
//        book.setCollege(infoDTO.getCollege());
        book.setCollege(studentInforDTO.getCollege());
////      book.setMajor(infoDTO.getMajor());
        book.setMajor(studentInforDTO.getMajor());

//        book.setNumber(infoDTO.getNumber());

        book.setName(userDTO.getName());
        String prefix = userDTO.getImg().substring(0, 8);  // 取前8位
        String suffix = userDTO.getImg().substring(userDTO.getImg().length() - 4);  // 取后4位
        String year =userDTO.getImg().substring(0, 4);
// 组合成新的文件名
        String newFileName = prefix + suffix;  // 新的文件名
//获取当前路径
        String currentWorkingDir = System.getProperty("user.dir");
        book.setPhoto(currentWorkingDir +"\\data\\"+ year +"\\"+ book.getMajor() +"\\"+ newFileName);
//        book.setPhoto(userDTO.getImg());
//        book.setSubmissionstatus(1);
        book.setCount(1);
        book.setIdcard(userDTO.getIdCard());
        if(infoDTO==null){
            studentDao.setstatus(userDTO);
            book.setNumber("");
            bookDao.addStudent(book);

        }else {
            studentDao.setstatus(userDTO);
            book.setNumber(infoDTO.getNumber());
            bookDao.addStudent(book);

        }
    }
    public List<Book> findAll() {
        return bookDao.selectAll();
    }

    public void updateStudent(UserDTO userDTO) {
        InfoDTO infoDTO =  typeDao.selectInforByIdCard(userDTO);
        studentInforDTO studentInforDTO=studentDao.selectstudentInforByIdCard(userDTO);
        Book book = new Book();
        book.setCollege(studentInforDTO.getCollege());
        book.setMajor(studentInforDTO.getMajor());
//        book.setNumber(infoDTO.getNumber());

        book.setName(userDTO.getName());
        String prefix = userDTO.getImg().substring(0, 8);  // 取前8位
        String suffix = userDTO.getImg().substring(userDTO.getImg().length() - 4);  // 取后4位
        String year =userDTO.getImg().substring(0, 4);
// 组合成新的文件名
        String newFileName = prefix + suffix;  // 新的文件名
//获取当前路径
        String currentWorkingDir = System.getProperty("user.dir");
        book.setPhoto(currentWorkingDir +"\\data\\"+ year +"\\"+ book.getMajor() +"\\"+ newFileName);


        book.setIdcard(userDTO.getIdCard());
        if(infoDTO==null){
            bookDao.updateStudent(book);
        }else {
            book.setNumber(infoDTO.getNumber());
            bookDao.updateStudent(book);
        }

    }

    public int selectCount(String number) {
        return bookDao.selectCountByNumber(number);
    }

    public Result fill() {
        //教务处的信息
        List<Type> type0 = typeDao.findAllType();
        // 一卡通的信息
        List<Book> book0 = bookDao.find0();



        // 创建一个映射以便快速查找教务处的学号

        if (type0.size()!=0){
            Map<String, String> studentIdToNumberMap = new HashMap<>();
            for (Type type : type0) {
                studentIdToNumberMap.put(type.getIdCard(), type.getNumber());
            }

            // 遍历一卡通信息并填充学号字段
            for (Book book : book0) {
                String studentId = book.getIdcard();
                if (studentIdToNumberMap.containsKey(studentId)) {
//                book.setNumber(studentIdToNumberMap.get(studentId));
                    String number = studentIdToNumberMap.get(studentId);
                    bookDao.updateNumber(studentId,number);
                }
            }
            return Result.success("填充成功");


        }else {
            return Result.error("教务处表未导入");
        }


    }

    public Book getInforByNumber(String studentNumber) {
        return bookDao.slectInforByNUmber(studentNumber);
    }


    public Integer selectCountByidcard(String idCard) {
        return bookDao.selectCountByidcard(idCard);
    }
}
